<?php
namespace app\admin\controller;

use app\common\controller\Base;
use ocean\Arr;
use think\Db;
use think\facade\View;

class Group extends Base

{
    protected $config=[
      'modelName'=>'Group'
    ];
    /*
     * 列表页
     */
    public function index()
    {
        //这里边需要选择关联模型的查询??,QQ提问
        $data=$this->model->select();

        View::share(['title'=>'用户组列表','data'=>$data]);
        return view();
    }
    /*
     *添加
     */
    public function addC(){
        //数据处理
        if($this->request->isPost()){
            $data=$this->request->post('','','strip_tags,trim');
            isset($data['rules'])&&$data['rules']=implode(',',$data['rules']);
            $check=$this->validation('app\admin\validate\Group',$data,'add');
            if($check['status']==0) $this->error('验证失败'.$check['msg']);
            $res=$this->model->save($data);
            ($res)?$this->success('添加组成功','index'):$this->error('添加组失败','index');
        }
        //分配相关数据到模板
        $arr=Db::table('think_auth_rule')->select();
        $arr=(new Arr())->channelLevel($arr);
        View::share(['title'=>'用户组管理','rules'=>$arr]);
        return view('store');
    }
    /*
     *编辑
     */
    public function editC($id){
        $id=intval($id);
        //更新数据
        if($this->request->isPost()){
            $data=$this->request->post('','','strip_tags,trim');
            $data['rules']=implode(',',$data['rules']);
            $data['id']=$id;
            $check=$this->validation('app\admin\validate\Group',$data, 'edit');
            if($check['status']==0) $this->error($check['msg']);
            $res=$this->model->save($data,$id);
            $res?$this->success('更新成功','index'):$this->error('更新失败'.$this->model->getError());
        }
        //分配旧数据
        $data=$this->model->where('id',$id)->find();
        $arr=Db::table('think_auth_rule')->select();
        $arr=(new Arr())->channelLevel($arr);
        View::share(['title'=>'用户组编辑','oldData'=>$data,'rules'=>$arr]);
        return view('store');
    }
    /*
     *删除
     */
    public function delC(){
        if($this->request->isPost()){
            $data=$this->request->post();
            $check=$this->validation('app\admin\validate\Delete',$data);
            if($check['status']==0) return '数据验证失败'.$check['msg'];
            // 删除组之前,先删除中间表数据
            //扩展改写为事务模式
                $this->model->id=$data['id'];
                $this->model->memberRel()->detach();
                $res=$this->model::destroy($data['id']);
               $back=$res?'删除成功':'删除失败'.$this->model->getError();
            return $back;
        }
    }
    /**
     * 设置成员
     */
    public function setMember($id){
        if($this->request->isPost()){
            $id=intval($id);
            $data=$this->request->post();
            // dump($data);
            empty($data)&&$this->error('请选择用户');
            // if($check['status']==0) return '数据验证失败'.$check['msg'];
            $this->model->id=$id;
            //先删除中间表数据,再进更新
            $this->model->memberRel()->detach();
            $res=$this->model->memberRel()->saveAll($data['uid']);
            ($res)?$this->success('设置成功','index'):$this->error('设置失败','index');
        }
        //分配数据到页面中
        // 待完善
        $member=Db::table('think_member')->field('id,mname')->where('status',1)->select();
        $oldData=Db::table('think_auth_group_access')->field('uid')->where('group_id',$id)->select();
        $oldData=array_column($oldData,'uid');
        // print_r($oldData);
        View::share(['title'=>'用户组成员设置','member'=>$member,'oldData'=>$oldData]);
        return View();
    }


}
